package com.xms.security.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.core.exception.XmsException;
import com.xms.core.model.KeyValuePair;
import com.xms.security.api.model.CopyRoleModel;
import com.xms.security.api.model.CreateRoleModel;
import com.xms.security.entity.RoleObjectAccess;
import com.xms.security.entity.Roles;
import com.xms.security.service.dataauth.IRoleObjectAccessService;
import com.xms.security.service.role.IRolesCreaterService;
import com.xms.security.service.role.IRolesFinderService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * RolesCreaterController
 * </p>
 *
 * @author migo
 * @since 2021/10/8
 */
@RestController
@RequestMapping("/{org}/api/security/role")
public class RolesCreaterController extends ApiBaseController {
    private IRolesCreaterService _rolesCreaterService;
    private IRolesFinderService _rolesFinderService;
    private IRoleObjectAccessService _roleObjectAccessService;

    @Autowired
    public void RolesCreaterController(IRoleObjectAccessService roleObjectAccessService,
                                       IRolesCreaterService rolesCreaterService,
                                       IRolesFinderService rolesFinderService) {
        _roleObjectAccessService = roleObjectAccessService;
        _rolesCreaterService = rolesCreaterService;
        _rolesFinderService = rolesFinderService;
    }

    @ApiOperation("新建安全角色")
    @PostMapping("create")
    public JsonResultObject create(@RequestBody CreateRoleModel model) throws XmsException, SQLException {
        if(StringUtil.isEmpty(model.name)){
            return JNotSpecifiedParams("name");
        }
        if(model.name.toLowerCase().contains("admin")){
            return JError("包含admin的名称已被系统保留，请另起名字");
        }
        UUID id = UUIDUtil.get();
        Roles entity = new Roles();
        entity.setRoleid(id);
        entity.setName(model.name);
        entity.setDescription(model.description);
        entity.setStatecode(true);
        entity.setCreatedon(LocalDate.now());
        entity.setCreatedby(_currentUser.getUser().SystemUserId);
        entity.setOrganizationid(_currentUser.getUser().OrganizationId);
        _rolesCreaterService.create(entity);
        return JCreateSuccess(new KeyValuePair<>("id", id));
    }

    @ApiOperation("复制角色")
    @PostMapping("copy")
    public JsonResultObject copyRole(@RequestBody CopyRoleModel model) throws XmsException, SQLException {
        Roles role = _rolesFinderService.getById(model.roleId);
        if (role != null) {
            Roles newRole = new Roles();
            newRole.setBusinessunitid(_currentUser.getUser().BusinessUnitId);
            newRole.setRoleid(UUIDUtil.get());
            newRole.setStatecode(true);
            newRole.setName(model.name);
            newRole.setCreatedon(LocalDate.now());
            newRole.setCreatedby(_currentUser.getUser().SystemUserId);
            newRole.setOrganizationid(_currentUser.getUser().OrganizationId);

            boolean flag = _rolesCreaterService.create(newRole);
            if (flag) {
                List<RoleObjectAccess> roaList = _roleObjectAccessService.list(
                        new LambdaQueryWrapper<RoleObjectAccess>().eq(RoleObjectAccess::getRoleid, model.roleId));
                if (CollectionUtil.notEmpty(roaList)) {
                    roaList.forEach(o ->
                    {
                        o.setRoleobjectaccessid(UUIDUtil.get());
                        o.setRoleid(newRole.getRoleid());
                    });
                    _roleObjectAccessService.saveBatch(roaList);
                }
                return JSaveSuccess();
            }
        }
        return JSaveFailure();
    }
}
